package com.smec.remes.module.submit.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.FragmentFinishBinding;
import com.smec.moblie.databinding.FragmentUnfinishBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.SubmitFinish;
import com.smec.remes.module.submit.SmecSubmitPresenter;
import com.smec.remes.module.submit.adapter.SmecSubmitAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;
import com.zyyoona7.lib.EasyPopup;
import com.zyyoona7.lib.HorizontalGravity;
import com.zyyoona7.lib.VerticalGravity;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/19.
 */

public class SmecFinishFragment extends SmecBaseFragment<SmecSubmitPresenter> {

    private SmecSubmitAdapter smecSubmitAdapter;
    private FragmentFinishBinding fragmentFinishBinding;
    private Handler mHandler=new Handler();
    public static final String FINISH="FINISH";
    public static final String FINISHFAIL="FINISHFAIL";
    public static final String RefrshFinish="RefrshFinish";
    private ArrayList<SubmitFinish> submitFinishArrayList=new ArrayList<>();
    private EasyPopup mCirclePop;
    private String  status;
    private int pageNumber=1;
    private String condition="";
    private String type;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentFinishBinding=DataBindingUtil.inflate(inflater, R.layout.fragment_finish,container,false);
        smecSubmitAdapter=new SmecSubmitAdapter(getActivity());
        fragmentFinishBinding.unfinishRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        fragmentFinishBinding.unfinishRecyclerView.setAdapter(smecSubmitAdapter);
        _firstGet();
        _titleBar();
        return fragmentFinishBinding.getRoot();
    }

    public void _titleBar(){
        fragmentFinishBinding.llChoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupWindow(fragmentFinishBinding.llChoice);
            }
        });
        fragmentFinishBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                searchByKeyWord();
            }
        });

        fragmentFinishBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    searchByKeyWord();
                }
                return false;
            }
        });

        fragmentFinishBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                fragmentFinishBinding.ivDelete.setVisibility(View.VISIBLE);
                fragmentFinishBinding.ivSearch.setImageResource(R.drawable.smec_remes_lightsearch);
            }
        });
        fragmentFinishBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                condition="";
                fragmentFinishBinding.edSearch.setText("");
            }
        });
    }

    private void searchByKeyWord(){
        if(TextUtils.isEmpty(condition=fragmentFinishBinding.edSearch.getText().toString())){
            condition="";
        }else {
            condition=fragmentFinishBinding.edSearch.getText().toString();
        }
        fragmentFinishBinding.ptr.startRefresh();
        CommonUtils.hideImmManager(fragmentFinishBinding.getRoot());
    }

    public void _firstGet(){
        fragmentFinishBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        fragmentFinishBinding.ptr.setBottomView(new LoadingView(getActivity()));
        fragmentFinishBinding.ptr.setAutoLoadMore(true);
        fragmentFinishBinding.ptr.setEnableOverScroll(false);
        fragmentFinishBinding.ptr.setEnableLoadmore(true);
        fragmentFinishBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNumber=1;
                type=_switch(fragmentFinishBinding.tvType.getText().toString());
                status="onRefresh";
                mPresenter._submitRequest(condition,type,pageNumber);
                pageNumber+=1;
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                status="onLoadMore";
                type=_switch(fragmentFinishBinding.tvType.getText().toString());
                mPresenter._submitRequest(condition,type,pageNumber);
                pageNumber+=1;
            }

        });
        fragmentFinishBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                fragmentFinishBinding.ptr.startRefresh();
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(FINISH)
            }
    )

    public void _finishSuccess(ArrayList<SubmitFinish> list){
        if(CommonUtils.notEmpty(list)){
            submitFinishArrayList=list;
            smecSubmitAdapter.setArrayList(list,status,"finish");
        }else {
            if(status.equals("onRefresh")){
                smecSubmitAdapter.clearList();
            }
        }
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    fragmentFinishBinding.ptr.finishRefreshing();
                }else {
                    fragmentFinishBinding.ptr.finishLoadmore();
                }
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(FINISHFAIL)
            }
    )

    public void _finishFail(String a){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    fragmentFinishBinding.ptr.finishRefreshing();
                }else {
                    fragmentFinishBinding.ptr.finishLoadmore();
                }
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(RefrshFinish)
            }
    )

    public void _RefrshFinish(String a){
        fragmentFinishBinding.ptr.startRefresh();
    }

    public static SmecFinishFragment newInstance(){
        Bundle args = new Bundle();
        SmecFinishFragment smecFinishFragment=new SmecFinishFragment();
        smecFinishFragment.setArguments(args);
        return smecFinishFragment;
    }

    @Override
    protected SmecSubmitPresenter getPersenter() {
        return new SmecSubmitPresenter(getActivity());
    }

    private void showPopupWindow(View view){
        mCirclePop = new EasyPopup(getActivity())
                .setContentView(R.layout.layout_choice_finish)
                .setAnimationStyle(R.style.QQPopAnim)
                .setFocusAndOutsideEnable(true)
                .setBackgroundDimEnable(false)
                .createPopup();
        mCirclePop.showAtAnchorView(view, VerticalGravity.BELOW, HorizontalGravity.LEFT, SizeUtils.dp2px(70), 0);
        LinearLayout smecCompare=mCirclePop.getView(R.id.smec_compare);
        smecCompare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mCirclePop("全部");
            }
        });
        LinearLayout smecDraft=mCirclePop.getView(R.id.smec_draft);
        smecDraft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mCirclePop("已完成");
            }
        });
        LinearLayout smecNote=mCirclePop.getView(R.id.smec_notes);
        smecNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _mCirclePop("已驳回");
            }
        });
    }

    public void _mCirclePop(String type){
        mCirclePop.dismiss();
        fragmentFinishBinding.tvType.setText(type);
        fragmentFinishBinding.ptr.startRefresh();
    }

    public String _switch(String a){
        String newType="";
        if(a.equals("全部")){
            newType="COMPLETEDORDER";
        }else if(a.equals("已完成")){
            newType="COMPLETED";
        }else {
            newType="REJECTED";
        }
        return  newType;
    }
}
